<template>
  <Modal
    title="编辑"
    :value="value"
    :mask-closable="true"
    :footer-hide="true"
    @input="$emit('input', $event)"
  >
    <Form
      ref="formTybkDeal"
      :model="formTybkDeal"
      :rules="ruleValidate"
      label-position="right"
      :label-width="80"
    >
      <FormItem label="成交信息标识" prop="id">
        <Input type="text" v-model="formTybkDeal.id" placeholder="成交信息标识"/>
      </FormItem>
      <FormItem label="公司名称" prop="corporate_name">
        <Input type="text" v-model="formTybkDeal.corporate_name" placeholder="公司名称"/>
      </FormItem>
      <FormItem label="资质标识" prop="qualification_id">
        <Input type="text" v-model="formTybkDeal.qualification_id" placeholder="资质标识"/>
      </FormItem>
      <FormItem label="合同签订日期" prop="deal_date_signing">
        <DatePicker type="date" v-model="formTybkDeal.deal_date_signing" style="width: 200px"></DatePicker>
      </FormItem>
      <FormItem label="下证日期" prop="deal_Issuing_date">
        <DatePicker type="date" v-model="formTybkDeal.deal_Issuing_date" style="width: 200px"></DatePicker>
      </FormItem>
      <FormItem label="年审提醒（提前三个月)" prop="deal_annual_review">
        <DatePicker type="date" v-model="formTybkDeal.deal_annual_review" style="width: 200px"></DatePicker>
      </FormItem>
      <FormItem label="到期提醒（提前三个月)" prop="deal_expire">
        <DatePicker type="date" v-model="formTybkDeal.deal_expire" style="width: 200px"></DatePicker>
      </FormItem>

      <FormItem>
        <Button type="primary" @click="handleSubmit('formTybkDeal')">提交</Button>
        <Button @click="handleReset('formTybkDeal')" style="margin-left: 8px">重置</Button>
      </FormItem>
    </Form>
  </Modal>
</template>
<script>
import { update } from '@/api/deal'
import { createModelObj, coverProps, coverFormProps } from '@/libs/util'

export default {
  name: 'Edit',
  props: {
    value: {
      type: Boolean,
      default: false
    },
    tybkDeal: {
      type: Object,
      default: null
    }
  },
  data () {
    return {
      formTybkDeal: {

        id: ''
        ,
        corporate_name: ''
        ,
        qualification_id: ''
        ,
        deal_date_signing: ''
        ,
        deal_Issuing_date: ''
        ,
        deal_annual_review: ''
        ,
        deal_expire: ''
        ,
        deleted: 0
        ,
        create_time: ''
        ,
        modification_time: ''
        ,
        deletion_time: ''
      },
      ruleValidate: {

        id: [
          { required: true, message: '该值不能为空', trigger: 'blur' },
          { type: 'string', max: 32, message: '长度最长为32', trigger: 'blur' }
        ]
        ,
        corporate_name: [
          { required: true, message: '该值不能为空', trigger: 'blur' },
          { type: 'string', max: 255, message: '长度最长为255', trigger: 'blur' }
        ]
        ,
        qualification_id: [
          { required: true, message: '该值不能为空', trigger: 'blur' },
          { type: 'string', max: 32, message: '长度最长为32', trigger: 'blur' }
        ]
        ,
      }
    }
  },
  methods: {
    handleSubmit (name) {
      this.$refs[name].validate(valid => {
        if (valid) {
          update(
            Object.assign({}, createModelObj(this.formTybkDeal, 'tybkDeal'))
          ).then(res => {
            if (res) {
              this.$emit('updateRow', coverProps(this.tybkDeal, this.formTybkDeal))
              this.$emit('input', false)
              this.handleReset(name)
            }
          })
        } else {
          this.$Message.error('验证失败!')
        }
      })
    },
    handleReset (name) {
      this.$refs[name].resetFields()
    }
  },
  watch: {
    value: function (val, oldVal) {
      this.$refs.formTybkDeal.resetFields()
      coverFormProps(this.formTybkDeal, this.tybkDeal)
    }
  }
}
</script>
